package com.cy.springcloudalibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @Description: 服务消费者
 * @ClassName: NacosDiscoverConsumerApplication
 * @Author: chenyu
 * @Date: 2025/1/10 10:38
 */
@EnableDiscoveryClient //开启服务注册于发现功能
@SpringBootApplication
public class NacosConfigApplication {
    
    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class, args);
        while (true) {
            // 当动态配置刷新时，会更新到environment，因此此处每隔一秒从environment中获取配置
            String userName = applicationContext.getEnvironment().getProperty("user.name");
            String userAge = applicationContext.getEnvironment().getProperty("user.age");
            System.out.println("user name : " + userName + "; age: " + userAge);

            // 获取当前部署的环境
            String curEnv = applicationContext.getEnvironment().getProperty("current.env");
            System.err.println("in "+ curEnv + " environment; " + "user name : " + userName + "; age: " + userAge);

            TimeUnit.SECONDS.sleep(1);
        }
    }
}
